<?php 
namespace app\common\controller;

use think\Controller;

class Upload extends Controller
{
	/**
	 * 上传base64位文件
	 * @param text $base64 64位编码
	 * @param unknown $address
	 * @return unknown|boolean
	 */
	public static function base64($base64,$address)
	{
		$address = $address.date('Y/m/d').'/';
		if(!is_dir($address)){
			mkdir($address,0777,true);
		}
		if (preg_match('/^(data:\s*image\/(\w+);base64,)/',$base64,$result)) {
			$ext = $result[2];
			$name = date('ymdHis') . rand(0, 1000);
			$filurl = $address."{$name}.{$ext}";
			if (file_put_contents($filurl, base64_decode(str_replace($result[1], '', $base64)))) {
				$url = substr($filurl,1);
				return $url;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}
}